<?php

class UserAddress{

	/**
	 * 添加
	 *
	 * @param unknown_type $name
	 * @param unknown_type $parent
	 * @param unknown_type $ord
	 */
	function add($data){
		$admin_id 	= admin_id_ajax();
		
		$data['created'] 	= full_date();
    	$data['admin_id'] 	= $admin_id;
    	
		$m = M('UserAddress');
		//返回自增id
		$id = $m->add($data);
		
		return $id;
	}
	
	
	function addAll($data){
		
		$created 	= full_date();
		$admin_id 	= admin_id_ajax();
		
		foreach ($data as $i => $d) {
			$data[$i]['created'] 	= full_date();
			$data[$i]['admin_id'] 	= $admin_id;
		}
    	
		$m = M('UserAddress');
		//返回自增id
		$id = $m->addAll($data);
		
		return $id;
	}
	
	
	/**
	 * 修改
	 *
	 * @param unknown_type $id
	 * @param unknown_type $upItems
	 * @return unknown
	 */
	function update($id,$upItems){
		$m = M('UserAddress');
		return $m -> where('id='.$id)->save($upItems);
	}
	
	function get_by_id($id){
		$m = M('UserAddress');
		return $m -> getById($id);
	}

	function lists($where,$page,$pageSize,&$allCount){

        $whereSql = ' 1=1 ';

        if(!is_null($where)){

            $where['end_time'] = short_date(strtotime($where['end_time']) + 86400);

            //查询条件
            $whereSql .= " AND (ua.created BETWEEN '{$where['begin_time']}' AND '{$where['end_time']}') ";

            if($where['search']){

                $item = $where['search']['item'];
                $value = $where['search']['value'];

                if($item != '' && $value != ''){
                    $whereSql .= " AND ua.{$item} LIKE '%{$value}%' ";
                }
            }
        }

        //开始行
		$startIndex = ($page-1) * $pageSize;
		
		//总计数
		$allCount = $this->lists_count($whereSql);
		
		$m = M('UserAddress');
		return $m->table('user_address ua')
		->join('admin a ON a.id = ua.admin_id')
		->join('users u ON u.user_id = ua.user_id')
        ->where($whereSql)
		->order('ua.id DESC')
		->limit($startIndex. ',' . $pageSize)
		->field('ua.*,a.real_name,u.user_name')
		->select();
	}
	
	
	function lists_count($whereSql){

	$m = M('UserAddress');
		return $m->table('user_address ua')
		->join('admin a ON a.id = ua.admin_id')
		->join('users u ON u.user_id = ua.user_id')
        ->where($whereSql)
		->order('ua.id ASC')
		->field('ua.*,a.real_name,u.user_name')
		->getField('COUNT(*)');
	}
	
	
	//删除
	function delete_by_id($id){
		$m = M('UserAddress');		
		$delete_result = $m -> where('id='.$id)->delete();			
		return $delete_result;
	}
	
	//最后的使用地址
	function get_last_address($number){
		$m = M('UserAddress');
		return $m->table('user_address')
		->where('disabled = 0')
		->order('last_use_time ASC')
		->limit('0,' . $number)
		->select();
	}
	
	//是否存在
	function exist($where){

		$m = M('UserAddress');
		return $m->table('user_address')
		->where("consignee = '{$where['consignee']}' OR address = '{$where['address']}' OR mobile_phone = '{$where['mobile_phone']}'")
		->find();
	}
	
/**
	 * 
	 * 导出的订单
	 * @param unknown_type $task_id
	 */
	function export_address(){
	
		$page   	= 1;
		
		$pageSize 	= PHP_INT_MAX;

    	$where = array(
            'begin_time'=> '2014-02-01',
            'end_time'  => date('Y-m-d')
        );

    	$allCount = 0;
    	
		$address = $this->lists($where,$page,$pageSize,$allCount);
		
		//标题
		$header = explode(',', '编号,收货人,收货地址,手机,状态,最后分配时间');
		
		$data[] = $header;
	
		
		foreach($address as $i => $o){

			$disabled = ($o['disabled']) ? '禁用':'启用';
			$last_use_time	= !empty($o['last_use_time']) ? $o['last_use_time']:'1970-01-01 00:00:00';
			$data[] = array(
				$o['id'],
				$o['consignee'],
				$o['address'],
				$o['mobile_phone'],
				$disabled,
				$last_use_time
			);
		}
		
		return $data;
	}
}